//获取url地址中的参数
function getSearchArg(argName) {
  // 去掉锚点连接
  let theHref = window.location.href; //http://localhost:3000/index.html?type=1#1234
  let searchStr;
  if (theHref.indexOf("#") > -1) {
      searchStr = theHref.substr(0, theHref.indexOf("#"));
  } else {
      searchStr = theHref.substr(0);
  }
   //searchStr  http://localhost:3000/index.html?type=1
  let [, arr = ""] = searchStr.split("?");
  let argArr = arr.split("&");
  for (let i = 0; i < argArr.length; i++) {
      let smallArgArr = argArr[i].split("=");
      if (smallArgArr[0] === argName) {
          return decodeURIComponent(smallArgArr[1]);
      }
  }
  return "";
}

const host = 'http://localhost:3000';
//ajxa的promise的封装
function sendAjax({type='get',
data=null,url,dataType='json'}){
    url = host + url;
    return new Promise((resolve,reject)=>{
        $.ajax({
            type,
            url,
            headers:{
                token:window.localStorage.getItem('token') || '',
            },
            data,
            dataType,
            success(data){
                resolve(data);
            },
            error(err){
                reject(err);
            }
        })
    })
}

//获取token中的mid
function getMid(){
    let token = window.localStorage.getItem('token') || '';
    if(!token){
        return;
    }
    let[,payload]=token.split('.');
    let {mid} = JSON.parse(window.atob(payload)) ;
    return mid;   
}
